﻿package component.validate.config;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import component.validate.util.StringUtil;

public class ValidatorInfo {
	public final static String TYPE_NORMAL = "normal";
	public final static String TYPE_TREE = "tree";
	
	private String id;
	private String table;
	private String type;	
	private String idField;	
	private String pathField;	
	private String title;
	
	private List deleteValList=new ArrayList();//DeleteValidatorInfo
	private List saveValList=new ArrayList();//SaveValidatorInfo
	
	public void setSaveValList(List saveValList) {
		this.saveValList = saveValList;
	}

	public void addDeleteValInfo(DeleteValidatorInfo delInfo){
		this.deleteValList.add(delInfo);
	}
	
	public List getDeleteValList() {
		return deleteValList;
	}

	public void setDeleteValList(List deleteValList) {
		this.deleteValList = deleteValList;
	}

	public List getSaveValList() {
		return saveValList;
	}
	
	public void addSaveValInfo(SaveValidatorInfo saveInfo){
		this.saveValList.add(saveInfo);
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getIdField() {
		return idField;
	}
	public void setIdField(String idField) {
		this.idField = idField;
	}
	public String getPathField() {
		return pathField;
	}
	public void setPathField(String pathField) {
		this.pathField = pathField;
	}
	
	public String[] getSaveValSql(Object[] conVals){
		int size = saveValList.size();
		String[] sqlArr = new String[size];
		for(int i=0;i<size;i++){
			SaveValidatorInfo saveVal = (SaveValidatorInfo)saveValList.get(i);
			String sql = saveVal.getSql();
			if(StringUtil.isNull(sql)){
				sql = " select " + this.getIdField() + "," + saveVal.getValFields() + " from " + table ;
				if(!StringUtil.isNull(saveVal.getCondition()))
					sql += " where " + saveVal.getCondition();
			}
			
			if(conVals!=null){
				Object[] conVals2 = conVals;
				if(conVals2[0].getClass().isArray()){//如果是多个验证(多个sql),conVals可能是二维数组
					conVals2 = (Object[])conVals[i];
				}
				for(int j=0;j<conVals2.length;j++){
					//可能有多个一样的值作为条件
					String paraValue = this.getConValue(conVals2[j]);
					//sql = sql.replaceFirst(":"+(j+1),paraValue);
					sql = sql.replaceAll(":"+(j+1),paraValue);
				}
			}
			sqlArr[i] = sql;
		}
		return sqlArr;
	}
	
	public Map getDeleteValSql(Object[] ids,Object[] cons){
		if(TYPE_TREE.equals(type)){
			return this.getTreeDeleteValSql(ids);
		}
		Map map = new HashMap();// <id,String[]>
		for(int k=0;k<ids.length;k++){
			String[] sqls = new String[deleteValList.size()];
			for(int i=0;i<deleteValList.size();i++){
				DeleteValidatorInfo delVal = (DeleteValidatorInfo)deleteValList.get(i);
				String sql = delVal.getSql();
				if(sql==null || "".equals(sql)){
					String[] relFieldArr = delVal.getRelField().split(",");
					String whereSql = " where 1=1 and (";
					for(int j=0;j<relFieldArr.length;j++){
						whereSql += relFieldArr[j] + "=" + this.getConValue(ids[k]);
						if(j<relFieldArr.length-1){ whereSql += " or "; }
					}
					whereSql += ")";
					sql = "select count(*) from " + delVal.getTable() + whereSql;
				}
				Object[] con = cons;
				//可能每个id对应的条件都不一样
				if(cons[k].getClass().isArray()){
					con = (Object[])cons[k];
				}
				for(int h=0;h<con.length;h++){
					sql = sql.replaceAll(":" + (h+1), getConValue(con[h]));
				}
				sqls[i] = sql;
			}
			map.put(ids[k], sqls);
		}
		return map;
	}
	
	private Map getTreeDeleteValSql(Object[] ids){
		Map map = new HashMap();// <id,String[]>
		for(int k=0;k<ids.length;k++){
			String conSql = "select " + this.idField + " from " + this.table + " where 1=1 ";

			conSql +=  " and " + this.pathField + " like '%"+ ids[0] + "%'";
//			for (int i = 1; i < ids.length; i++) {
//				conSql += " or " + this.pathField + " like '%" + ids[i] + "%'";
//			}
			
			String[] sqls = new String[deleteValList.size()];
			for(int i=0;i<deleteValList.size();i++){
				DeleteValidatorInfo delVal = (DeleteValidatorInfo)deleteValList.get(i);
				String relField = delVal.getRelField();
				String whereSql = " where 1=1 and " + relField + " in (" + conSql + ")";
				sqls[i] = "select count(*) from " + delVal.getTable() + whereSql;
			}
			map.put(ids[k], sqls);
		}
		return map;
	}
	
	private String getConValue(Object value){
		if(value instanceof String){
			return "'" + value + "'";
		}else{
			return String.valueOf(value);
		}
	}
	
	public static void main(String[] args) {
		System.out.println(":1 asf :1 ".replaceAll(":1", "yy"));
	}
}
